
package com.wyzz.hr.bo.impl;

import java.io.PrintStream;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.ITremployeeplanBO;
import com.wyzz.hr.dao.IStatusDAO;
import com.wyzz.hr.dao.ITrcourseDAO;
import com.wyzz.hr.dao.ITrcourseplanDAO;
import com.wyzz.hr.dao.ITremployeeplanDAO;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Trcourse;
import com.wyzz.hr.pojo.Trcourseplan;
import com.wyzz.hr.pojo.Tremployeeplan;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.vo.TrainingSearchBean;


public class TremployeeplanBOImpl extends EmpApproverBoImpl implements
		ITremployeeplanBO {

	public TremployeeplanBOImpl() {
	}

	public boolean delete(String trepId) {
		try {
			tremployeeplanDAO.deleteObject(
					com.wyzz.hr.pojo.Tremployeeplan.class, trepId);
			sl_BO.deleteLogs("trainrequest", trepId);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public Tremployeeplan loadById(String trepId, String fetch[]) {
		return (Tremployeeplan) tremployeeplanDAO.loadObject(
				com.wyzz.hr.pojo.Tremployeeplan.class, trepId, fetch,
				new boolean[0]);
	}

	public void saveOrupdate(Tremployeeplan trep) {
		trep.setTrpLastChangeTime(new Date());
		tremployeeplanDAO.saveOrupdate(trep);
	}

	public boolean hasApplied(String empNo, String trcpId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Tremployeeplan.class);
		detachedCriteria.setFetchMode(Tremployeeplan.PROP_TRP_TRAINEE_NO,
				FetchMode.JOIN).setFetchMode(Tremployeeplan.PROP_TRP_TRCP,
				FetchMode.JOIN);
		if (empNo != null && empNo.trim().length() > 0)
			detachedCriteria.add(Restrictions.eq((new StringBuilder()).append(
					Tremployeeplan.PROP_TRP_TRAINEE_NO).append(".").append(
					Employee.PROP_ID).toString(), empNo.trim()));
		if (trcpId != null)
			detachedCriteria.add(Restrictions.eq((new StringBuilder()).append(
					Tremployeeplan.PROP_TRP_TRCP).append(".").append(
					Trcourseplan.PROP_TRCP_ID).toString(), trcpId));
		return tremployeeplanDAO.findByCriteria(detachedCriteria).size() > 0;
	}

	public int countAppliedAmount(String trcpId) {
		String hql = (new StringBuilder()).append(
				"select count(*) from Tremployeeplan where trp_trcp_id='")
				.append(trcpId).append("' and trp_status !=").append(21)
				.append(" and trp_status !=").append(22).toString();
		return ((Long) tremployeeplanDAO.exeHqlList(hql).get(0)).intValue();
	}

	public int hasAppliedEmp(String trcpId) {
		String hql = (new StringBuilder()).append(
				"select count(*) from Tremployeeplan where trp_trcp_id='")
				.append(trcpId).append("'").toString();
		return ((Long) tremployeeplanDAO.exeHqlList(hql).get(0)).intValue();
	}

	public ITremployeeplanDAO getTremployeeplanDAO() {
		return tremployeeplanDAO;
	}

	public void setTremployeeplanDAO(ITremployeeplanDAO tremployeeplanDAO) {
		this.tremployeeplanDAO = tremployeeplanDAO;
	}

	public ITrcourseDAO getTrcourseDAO() {
		return trcourseDAO;
	}

	public void setTrcourseDAO(ITrcourseDAO trcourseDAO) {
		this.trcourseDAO = trcourseDAO;
	}

	public ITrcourseplanDAO getTrcourseplanDAO() {
		return trcourseplanDAO;
	}

	public void setTrcourseplanDAO(ITrcourseplanDAO trcourseplanDAO) {
		this.trcourseplanDAO = trcourseplanDAO;
	}

	public IStatusDAO getStatusDAO() {
		return statusDAO;
	}

	public void setStatusDAO(IStatusDAO statusDAO) {
		this.statusDAO = statusDAO;
	}

	public List loadByIds(String trIds[], String fetch[]) {
		List result;
		try {
			DetachedCriteria dc = DetachedCriteria
					.forClass(com.wyzz.hr.pojo.Tremployeeplan.class);
			for (int i = 0; i < fetch.length; i++)
				dc.setFetchMode(fetch[i], FetchMode.JOIN);

			BaseCrit.addDC(dc, Tremployeeplan.PROP_TRP_ID, "in", trIds);
			dc.addOrder(Order.asc(Tremployeeplan.PROP_TRP_NO));
			result = tremployeeplanDAO.findByCriteria(dc);
			if (result.size() == 0)
				return null;
		} catch (Exception e) {
			return null;
		}
		return result;
	}

	public List loadByDc(DetachedCriteria dc) {
		List result;
		try {
			result = tremployeeplanDAO.findByCriteria(dc);
			if (result.size() == 0)
				return null;
		} catch (Exception e) {
			return null;
		}
		return result;
	}

	public void trainingSearch(TrainingSearchBean searchBean,
			DetachedCriteria dc, Pager page) {
		dc.createAlias(Tremployeeplan.PROP_TRP_TRAINEE_NO, "trpTraineeNo", 1);
		dc.createAlias(Tremployeeplan.PROP_TRP_CREATE_BY, "trpCreateBy");
		dc.createAlias(Tremployeeplan.PROP_TRP_TRCP, "trpTrcp", 1);
		dc.createAlias((new StringBuilder()).append(
				Tremployeeplan.PROP_TRP_TRCP).append(".").append(
				Trcourseplan.PROP_TRCP_COORDINATOR).toString(),
				"trcpCoordinator", 1);
		dc
				.createAlias((new StringBuilder()).append(
						Tremployeeplan.PROP_TRP_TRCP).append(".").append(
						Trcourseplan.PROP_TRCP_COURSE_NO).toString(),
						"trcpCourseNo", 1);
		dc.createAlias((new StringBuilder()).append(
				Tremployeeplan.PROP_TRP_TRCP).append(".").append(
				Trcourseplan.PROP_TRCP_TRINS_ID).toString(), "trcpTrinsId", 1);
		dc.createAlias((new StringBuilder()).append(
				Tremployeeplan.PROP_TRP_TRCP).append(".").append(
				Trcourseplan.PROP_TRCP_COURSE_NO).append(".").append(
				Trcourse.PROP_TRC_TYPE).toString(), "trcType", 1);
		dc.createAlias((new StringBuilder()).append(
				Tremployeeplan.PROP_TRP_TRAINEE_NO).append(".").append(
				Employee.PROP_EMP_PB_NO).toString(), "empPbNo", 1);
		try {
			if (!StringUtils.isEmpty(searchBean.getTrpNo()))
				BaseCrit.addDC(dc, Tremployeeplan.PROP_TRP_NO, "eq",
						new Integer[] { Integer.valueOf(Integer
								.parseInt(searchBean.getTrpNo())) });
		} catch (Exception ex) {
			System.out.println((new StringBuilder()).append(
					"==========error: cannot calc No").append(
					searchBean.getTrpNo()).toString());
		}
		BaseCrit.addDC(dc, (new StringBuilder()).append(
				Tremployeeplan.PROP_TRP_TRCP).append(".").append(
				Trcourseplan.PROP_TRCP_LOCATION).toString(), "like",
				new String[] { searchBean.getTrcpLocation() });
		String alias[] = {
				(new StringBuilder()).append(Tremployeeplan.PROP_TRP_TRCP)
						.append(".trcpStartDate").toString(),
				(new StringBuilder()).append(Tremployeeplan.PROP_TRP_TRCP)
						.append(".trcpEndDate").toString() };
		BaseCrit.addDCTwoDateRange(dc, alias, searchBean.getStartDate(),
				searchBean.getEndDate());
		if (searchBean.getEmpdept() != null)
			BaseCrit.addDeptDC(dc, (new StringBuilder()).append(
					Tremployeeplan.PROP_TRP_TRAINEE_NO).append(".").append(
					Employee.PROP_EMP_DEPT_NO).toString(),
					(new StringBuilder()).append(
							Tremployeeplan.PROP_TRP_TRAINEE_NO).append(".")
							.append(Employee.PROP_EMP_PB_NO).toString(),
					Integer.valueOf(1), searchBean.getEmpdept().getEmpDeptNo());
		BaseCrit.addDC(dc, Tremployeeplan.PROP_TRP_STATUS, "eq",
				new Integer[] { searchBean.getStatu() });
		BaseCrit.addEmpDC(dc, Tremployeeplan.PROP_TRP_TRAINEE_NO, searchBean
				.getEmp());
		if (searchBean.getTrcp() != null
				&& searchBean.getTrcp().getTrcpCourseNo() != null)
			BaseCrit.addDC(dc, (new StringBuilder()).append("trcpCourseNo.")
					.append(Trcourse.PROP_TRC_NAME).toString(), "like",
					new String[] { searchBean.getTrcp().getTrcpCourseNo()
							.getTrcName().trim() });
		if (searchBean.getTrcp() != null
				&& searchBean.getTrcp().getTrcpTrinsId() != null)
			BaseCrit.addDC(dc, "trcpTrinsId.trinsId", "eq",
					new String[] { searchBean.getTrcp().getTrcpTrinsId()
							.getTrinsId().trim() });
		BaseAction
				.addOrders(dc, page, new String[] { (new StringBuilder())
						.append(Tremployeeplan.PROP_TRP_NO).append("-down")
						.toString() });
		page.splitPage(dc);
		List result = tremployeeplanDAO.findByCriteria(dc, page.getPageSize(),
				page.getCurrentPage());
		searchBean.setResult(result);
	}

	public void searchEmpPlan(DetachedCriteria dc,
			TrainingSearchBean searchBean, Pager page) {
		trainingSearch(searchBean, dc, page);
	}

	public boolean deleteAttach(String contractId, String fileName) {
		try {
			FileOperate.deleteFile("sys.training.file.path", fileName);
			String hql = (new StringBuilder()).append(
					"update Tremployeeplan set trpFileName=null where trpId='")
					.append(contractId).append("'").toString();
			tremployeeplanDAO.exeHql(hql);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public List getTrepByHql(String hql) {
		return tremployeeplanDAO.exeHqlList(hql);
	}

	private ITremployeeplanDAO tremployeeplanDAO;
	private ITrcourseDAO trcourseDAO;
	private ITrcourseplanDAO trcourseplanDAO;
	private IStatusDAO statusDAO;
}
